package com.lemon.reggie.handle;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.lemon.reggie.utils.BaseContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class MyMetaObjectHandlerHandle implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("------当前请求的线程id为:{}", Thread.currentThread().getId());
        log.info("[insert]公共字段填充...");
        if (metaObject.hasSetter("createTime")) {
            metaObject.setValue("createTime", LocalDateTime.now());
        }

        if (metaObject.hasSetter("updateTime")) {
            metaObject.setValue("updateTime", LocalDateTime.now());
        }

        if (metaObject.hasSetter("createUser")) {
            metaObject.setValue("createUser", BaseContextUtil.getCurrentId());
        }

        if (metaObject.hasSetter("updateUser")) {
            metaObject.setValue("updateUser", BaseContextUtil.getCurrentId());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("------当前请求的线程id为:{}", Thread.currentThread().getId());
        log.info("[update]公共字段填充...");
        if (metaObject.hasSetter("updateTime")) {
            metaObject.setValue("updateTime", LocalDateTime.now());
        }

        if (metaObject.hasSetter("updateUser")) {
            metaObject.setValue("updateUser", BaseContextUtil.getCurrentId());
        }
    }
}
